package com.sam.im.heiqun.uis.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.ImFriendEntivity;
import com.sam.im.heiqun.uis.activities.ChoseGroupActivity;
import com.sam.im.heiqun.uis.activities.NewFriendsActivity;
import com.sam.im.heiqun.uis.adapters.holder.FriendGroupHolder;
import com.sam.im.heiqun.uis.adapters.holder.FriendHolder;
import com.sam.im.heiqun.uis.widgets.sidebar.CountrySortModel;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Created by wangguodong on 2017/1/8.
 * 群成员使用
 */

public class FriendGroupAdpter extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {

    private Context context;
    private List<CountrySortModel> mFrientEntivities;
    private FriendListClickListener mItemClickListener;
    private int type = 0;//判断选择框是否显示；0-不显示；1-显示;2-不显示
    private boolean hintHeadview  ;
    //private ImFriendEntivity imFriendEntivityK;

    private List<ImFriendEntivity> imFriendEntivities = new ArrayList<>();

    private OnLongClickListener mOnLongClickListener ;
    public interface OnLongClickListener{
        boolean OnLongClick(int pos);
    }

    public void setOnLongClickListener(OnLongClickListener onLongClickListener){
        mOnLongClickListener = onLongClickListener ;
    }

    public void setHintHeadview(boolean hintHeadview) {
        this.hintHeadview = hintHeadview;
        notifyDataSetChanged();
    }

    public FriendGroupAdpter(Context context, List<CountrySortModel> mFrientEntivities) {
        this.context = context;
        this.mFrientEntivities = mFrientEntivities;
    }



    public FriendGroupAdpter(Context context) {
        this.context = context;
    }

    public void refresh(List<CountrySortModel> mFrientEntivities) {
        this.mFrientEntivities = mFrientEntivities;
        notifyDataSetChanged();
    }





    public interface FriendListClickListener {
        void onAgreeNewFriendClick(View view, int position);
    }

    public void setItemClickListener(FriendListClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        //switch (viewType){
           // case 0 :
             /*   View view1 = LayoutInflater.from(context).inflate(R.layout.friend_head_item,parent,false);
                return new HeadHolder(view1);*/
           // default:
                View view2 = LayoutInflater.from(context).inflate(R.layout.child_item_layout, parent, false);

                return new FriendGroupHolder(view2, mItemClickListener);

        //}

    }


    class HeadHolder extends RecyclerView.ViewHolder{
        LinearLayout newfriends;
        LinearLayout groups ;
        LinearLayout headlayout ;

        public HeadHolder(View itemView) {
            super(itemView);
            newfriends = (LinearLayout) itemView.findViewById(R.id.new_friends);
            groups = (LinearLayout) itemView.findViewById(R.id.group);
            headlayout = (LinearLayout) itemView.findViewById(R.id.head_layout);
            //img = (ImageView) view2.findViewById(R.id.img);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
/*
        if (position==0){
            setheadlayout((HeadHolder)holder,position);
        }else {*/
            setitemlayout((FriendGroupHolder)holder,position);

      //  }

    }

    private void setitemlayout(FriendGroupHolder holder, final int position) {


        if (1 == type) {
            holder.state_image.setVisibility(View.VISIBLE);
            if (mFrientEntivities.get(position-1).isSelecte) {
                holder.state_image.setBackgroundResource(R.drawable.checkbox_partial);

            } else {
                holder.state_image.setBackgroundResource(R.drawable.checkbox_unchecked);
            }
        } else {
            holder.state_image.setVisibility(View.GONE);
        }
        int num = position-1 ;
        GlideUtils.loadHeadCircularImage(context, mFrientEntivities.get(num).getHeadUrl(), holder.head_image);
        holder.name.setText(ToolsUtils.getNick(mFrientEntivities.get(num)));
     //   holder.text_pinyin.setText(mFrientEntivities.get(num).sortLetters);
        holder.item_detail.setVisibility(View.GONE);
     /*   if (num > 0) {
            if (mFrientEntivities.get(num - 1).sortLetters.equals(mFrientEntivities.get(num).sortLetters)) {
                holder.text_pinyin.setVisibility(View.GONE);
            } else {
                holder.text_pinyin.setVisibility(View.VISIBLE);
            }
        } else {
            holder.text_pinyin.setVisibility(View.VISIBLE);
        }*/

        holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (null!=mOnLongClickListener)return mOnLongClickListener.OnLongClick(position - 1);
                return false;
            }
        });
//image_head_tip
        Log.i("group--", "setitemlayout: =======群组001=================mFrientEntivities.get(num).getRole()="+mFrientEntivities.get(num).getRole());
        if (null!=mOnLongClickListener){
            Log.i("group--", "setitemlayout: =======群组002==================");
            if ("1".equals(mFrientEntivities.get(num).getRole())){
                Log.i("group--", "setitemlayout: =======群组003==================");
                holder.image_head_tip.setBackgroundResource(R.drawable.group_create);
                holder.image_head_tip.setVisibility(View.VISIBLE);
            }else
            if ("2".equals(mFrientEntivities.get(num).getRole())){
                Log.i("group--", "setitemlayout: =======群组004==================");
                holder.image_head_tip.setBackgroundResource(R.drawable.group_manager);
                holder.image_head_tip.setVisibility(View.VISIBLE);
            }else {
                Log.i("group--", "setitemlayout: =======群组005==================");
                holder.image_head_tip.setVisibility(View.GONE);
            }
        }
            Log.i("group--", "setitemlayout: =======群组006==================");

    }

    private void setheadlayout(HeadHolder holder, int position) {
     //   if (hintHeadview){
            holder.headlayout.setVisibility(View.GONE);
            holder.newfriends.setVisibility(View.GONE);
            holder.groups.setVisibility(View.GONE);
       /* }else {
            holder.headlayout.setVisibility(View.VISIBLE);
            holder.newfriends.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    context.startActivity(new Intent(context,NewFriendsActivity.class));
                }
            });
            holder.groups.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context,ChoseGroupActivity.class);
                    intent.putExtra("type", 1);
                    context.startActivity(intent);
                }
            });
        }*/

    }
    @Override
    public int getItemCount() {
        return mFrientEntivities != null ? mFrientEntivities.size()+1 : 0;
    }





}